package utils

import (
	"github.com/dgrijalva/jwt-go"
	"im-server-go/entity/mysql_model"
	"time"
)

// GenToken 生成token
func GenToken(telephone string) (string, error) {
	user := mysql_model.NewUserCriteria().AndTelephoneEqualTo(telephone).First()
	claims := jwt.MapClaims{}
	claims.VerifyExpiresAt(time.Now().AddDate(0, 0, 1).Unix(), true)
	claims.VerifyIssuer("cim", true)
	claims["user_id"] = user.UserId

	token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
	signedString, err := token.SignedString("cim")
	if err != nil {
		return "", err
	}
	return signedString, nil
}
